package com.qiaofang.service.impl;

import com.qiaofang.common.constant.DelFlagEnum;
import com.qiaofang.common.constant.ExceptionEnum;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.utils.StringUtil;
import com.qiaofang.constant.DepartmentConstant;
import com.qiaofang.dto.DepartmentDTO;
import com.qiaofang.entity.Company;
import com.qiaofang.entity.Department;
import com.qiaofang.repository.DepartmentRepository;
import com.qiaofang.service.DepartmentService;
import com.qiaofang.vo.DepartmentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void addTopDepartment(Company company) {
        DepartmentDTO departmentDTO = new DepartmentDTO();
        departmentDTO.setDepartmentCode(String.valueOf(company.getId()));
        departmentDTO.setDepartmentName(company.getCompanyName());
        departmentDTO.setMerchantId(company.getId());
        //顶级部门
        departmentDTO.setLayer(DepartmentConstant.TOP.intValue());
        departmentDTO.setParentId(DepartmentConstant.TOP);
        addDepartment(departmentDTO);
    }

    @Override
    public void addDepartment(DepartmentDTO departmentDTO) {
        Department department = new Department();
        department.setDepartmentCode(departmentDTO.getDepartmentCode());
        department.setParentId(departmentDTO.getParentId());
        department.setDepartmentName(departmentDTO.getDepartmentName());
        department.setMerchantId(departmentDTO.getMerchantId());
        department.setLayer(departmentDTO.getLayer());
        department.setDelFlag(DelFlagEnum.UNDEL.getNo());
        department.setPrincipal(departmentDTO.getPrincipal());
        department = departmentRepository.save(department);

        department.setPermissionCode(String.valueOf(department.getId()));
        departmentRepository.save(department);
    }

    @Override
    public void updateTopDepartment(Company company, String partnerInfoId) {
        DepartmentDTO departmentDTO = new DepartmentDTO();
        departmentDTO.setParentId(DepartmentConstant.TOP);
        departmentDTO.setMerchantId(company.getId());
        departmentDTO.setDepartmentName(company.getCompanyName());
        Department example = new Department();
        example.setMerchantId(departmentDTO.getMerchantId());
        example.setParentId(departmentDTO.getParentId());
        List<Department> departments = departmentRepository.findAll(Example.of(example));
        if (departments != null && departments.size() == 1) {
            Department department = departments.get(0);
            department.setDepartmentName(departmentDTO.getDepartmentName());
            if (StringUtil.isNotBlank(partnerInfoId)) department.setPrincipal(Long.valueOf(partnerInfoId));
            departmentRepository.save(department);
        } else {
            throw new BusinessException(ExceptionEnum.TOP_DEPARTMENT);
        }
    }

    @Override
    public DepartmentVO getDepartmentByDTO(DepartmentDTO departmentDTO) {
        Department example = new Department();
        example.setMerchantId(departmentDTO.getMerchantId());
        example.setParentId(departmentDTO.getParentId());
        List<Department> departments = departmentRepository.findAll(Example.of(example));
        if (departments != null && departments.size() > 0) {
            return convertDomain2VO(departments.get(0));
        }
        return null;
    }

    private DepartmentVO convertDomain2VO(Department department) {
        DepartmentVO vo = new DepartmentVO();
        vo.setId(department.getId());
        return vo;
    }

    @Override
    public List<Department> listTopDepartment(List<Long> companyIds) {
        return departmentRepository.listTopDepartment(companyIds);
    }
}
